﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class RequestCounter
    {
        public static Dictionary<string, RequestCounter> Counters = new Dictionary<string, RequestCounter>();
        public const int MaxRequestCount = 1;
        public const int ResetSeconds = 10;
        public int CurrentCount
        {
            get;
            private set;
        }
        public string UserId { get; set; }
        private DateTime _lastResetTime;
        public bool AddCount()
        {
            DateTime now = DateTime.Now;
            if ((now - _lastResetTime).Seconds >= ResetSeconds)
            {
                CurrentCount = 0;
                _lastResetTime = now;
            }
            CurrentCount++;
            if (CurrentCount <= MaxRequestCount)
            {
                return true;
            }
            return false;
        }

        public static RequestCounter GetCounter(string userId)
        {
            if (Counters.ContainsKey(userId))
            {
                return Counters[userId];
            }
            else
            {
                Counters[userId] = new RequestCounter();
                return Counters[userId];
            }
        }
    }
}
